﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace CharacterCreation.Controls {
    /// <summary>
    /// The computron ListBoxItem
    /// </summary>
    [TemplateVisualState(GroupName = "Custom", Name = "MouseOver")]
    [TemplatePart(Type = typeof(ContentPresenter))]
    public class ListBoxItem : ContentControl {

        private ContentPresenter _contentPresenter = null;

        /// <summary>
        /// Gets or sets the IsSelected
        /// </summary>
        public bool IsSelected {
            get { return _IsSelected; }
            set { _IsSelected = value; setTemplate(value); }
        }
        private bool _IsSelected;

        /// <summary>
        /// Gets or sets the ListBox
        /// </summary>
        public ListBox ListBox {
            get { return _ListBox; }
            set { _ListBox = value; }
        }
        private ListBox _ListBox;

        /// <summary>
        /// The CustomItemTemplate property.
        /// </summary>
        public DataTemplate CustomItemTemplate {
            get {
                DataTemplate template = GetValue(CustomItemTemplateProperty) as DataTemplate;
                if (template == null) {
                    template = (DataTemplate)(this.GetTemplateChild("grid") as Grid).Resources["defaultTemplate"];
                    SetValue(CustomItemTemplateProperty, template);
                }
                return template;
            }
            set { SetValue(CustomItemTemplateProperty, value); }
        }
        public static readonly DependencyProperty CustomItemTemplateProperty =
            DependencyProperty.Register("CustomItemTemplate", typeof(DataTemplate), typeof(ListBoxItem), null);

        /// <summary>
        /// The SelectedItemTemplate property.
        /// </summary>
        public DataTemplate SelectedItemTemplate {
            get {
                DataTemplate template = GetValue(SelectedItemTemplateProperty) as DataTemplate;
                if (template == null) {
                    template = (DataTemplate)(this.GetTemplateChild("grid") as Grid).Resources["defaultSelectedTemplate"];
                    SetValue(SelectedItemTemplateProperty, template);
                }
                return template;
            }
            set { SetValue(SelectedItemTemplateProperty, value); }
        }
        public static readonly DependencyProperty SelectedItemTemplateProperty =
            DependencyProperty.Register("SelectedItemTemplate", typeof(DataTemplate), typeof(ListBoxItem), null);


        /// <summary>
        /// The HoverItemTemplate property.
        /// </summary>
        public DataTemplate HoverSelectedItemTemplate {
            get { return (DataTemplate)GetValue(HoverSelectedItemTemplateProperty); }
            set { SetValue(HoverSelectedItemTemplateProperty, value); }
        }
        public static readonly DependencyProperty HoverSelectedItemTemplateProperty =
            DependencyProperty.Register("HoverSelectedItemTemplate", typeof(DataTemplate), typeof(ListBoxItem), null);


        /// <summary>
        /// The HoverItemTemplate property.
        /// </summary>
        public DataTemplate HoverItemTemplate {
            get { return (DataTemplate)GetValue(HoverItemTemplateProperty); }
            set { SetValue(HoverItemTemplateProperty, value); }
        }
        public static readonly DependencyProperty HoverItemTemplateProperty =
            DependencyProperty.Register("HoverItemTemplate", typeof(DataTemplate), typeof(ListBoxItem), null);

        /// <summary>
        /// ctor
        /// </summary>
        static ListBoxItem() {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ListBoxItem), new FrameworkPropertyMetadata(typeof(ListBoxItem)));
           
        }

        private bool clicked = false;
        void CTRONListBoxItem_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            clicked = true;
        }

        void CTRONListBoxItem_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) {
            if (clicked) {
                if (ListBox != null) {
                    this.IsSelected = !this.IsSelected;
                    if (this.IsSelected) {
                        if (ListBox.SelectedItem != null &&
                            ListBox.SelectedListBoxItem != null &&
                            !ListBox.AllowMultiSelect)
                            ListBox.SelectedListBoxItem.IsSelected = false;

                        ListBox.SelectedItem = this.DataContext;
                        ListBox.SelectedListBoxItem = this;

                        if (ListBox.AllowMultiSelect)
                            ListBox.SelectedItems.Add(this.Content);
                    }
                    else {
                        ListBox.SelectedItem = null;
                        ListBox.SelectedListBoxItem = null;

                        if (ListBox.AllowMultiSelect)
                            ListBox.SelectedItems.Remove(this.Content);
                    }
                }
                else throw new Exception("No parent defined");
            }

            clicked = false;
        }

        void CTRONListBoxItem_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e) {
            clicked = false;
            this.setTemplate(this.IsSelected);
        }

        void CTRONListBoxItem_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e) {
            if (this.IsSelected && this.HoverSelectedItemTemplate != null)
                _contentPresenter.ContentTemplate = this.HoverSelectedItemTemplate;
            else if (!this.IsSelected && this.HoverItemTemplate != null)
                _contentPresenter.ContentTemplate = this.HoverItemTemplate;
        }

        public override void OnApplyTemplate() {
            base.OnApplyTemplate();
            _contentPresenter = (ContentPresenter)this.GetTemplateChild("contentPresenter");
            this.IsSelected = false;
            this.MouseEnter += new System.Windows.Input.MouseEventHandler(CTRONListBoxItem_MouseEnter);
            this.MouseLeave += new System.Windows.Input.MouseEventHandler(CTRONListBoxItem_MouseLeave);
            this.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(CTRONListBoxItem_MouseLeftButtonDown);
            this.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(CTRONListBoxItem_MouseLeftButtonUp);
        }

        private void setTemplate(bool value) {
            if (value) _contentPresenter.ContentTemplate = this.SelectedItemTemplate;
            else _contentPresenter.ContentTemplate = this.CustomItemTemplate;
        }
    }
    
}
